#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File  :tests_page1.py
# Time  :2025/3/26 17:48
# Email :fortunatedong@163.com
# Author:by Adonli
import json

import streamlit as st
import pandas as pd
import os

from config import ROOT_DIR
from config import Header_space
Header_space()
sku_url_path = os.path.join(ROOT_DIR, 'Datas', 'sku_url.csv')
st.subheader("编辑国铁京东SKU信息", divider=True)

if os.path.exists(sku_url_path):
    try:
        df = pd.read_csv(sku_url_path,index_col=False,dtype=str, keep_default_na=False)
    except Exception as e:
        df = pd.DataFrame(
            [
                {"SKU编号": "", "商品名称": "", "国铁价格": "", "京东URL": ""},
            ]
        )
        df.to_csv(sku_url_path,index=False)
    finally:
        df = pd.read_csv(sku_url_path,index_col=False,dtype=str, keep_default_na=False)
    # print(df)
else:
    df = pd.DataFrame(
        [
            {"SKU编号": "", "商品名称": "", "国铁价格": "", "京东URL": ""},
        ]
    )
    df.to_csv(sku_url_path, index=False)
    df = pd.read_csv(sku_url_path, index_col=False, dtype=str, keep_default_na=False)
config = {
    'SKU编号' : st.column_config.TextColumn(),
    '商品名称' : st.column_config.TextColumn(),
    '国铁价格' : st.column_config.TextColumn(),
    '京东URL' : st.column_config.LinkColumn()
}
result = st.data_editor(df,column_config=config,num_rows='dynamic')
if st.button('保存'):
    result.to_csv(sku_url_path,index=False)
    st.write(result)

